<template>
  <div class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item v-if="!id" label="上级部门" prop="parentName">
          <div class="show">{{ formData.parentName }}</div>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="formData.deptName" placeholder="请输入" clearable />
        </el-form-item>

        <el-form-item v-if="id&&formData.parentId" label="上级部门" prop="deptId">
          <el-select v-model="formData.parentId" filterable placeholder="请选择">
            <el-option
              v-for="item in deptOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { add as save, allExcludeChildren as getDept, edit as update, getInfo } from '@/api/system/dept'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    defaultValue: {},
    id: {}
  },
  data() {
    return {
      formData: {
        deptId: undefined,
        parentId: undefined,
        parentName: undefined,
        deptName: undefined
      },
      rules: {
        deptName: [
          { required: true, message: '部门名称不能为空', trigger: 'change' }
        ]
      },
      deptOptions: []
    }
  },
  created() {
    if (!this.id) { // 没有data就是新增
      Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
        this.formData[key] = this.defaultValue[key]
      })
    } else { // 有data就是编辑
      // this.formData.deptId = this.data.id
      // this.formData.deptName = this.data.label
      this.getById()
      this.getDept()
    }
  },
  methods: {
    getDept() {
      getDept({ status: 1, currentDeptId: this.id }).then(res => {
        this.deptOptions = res.data
      })
    },
    getById() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
      })
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.id) { // 没有id就是新增
            save(this.formData).then(res => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$layer.close(this.layerid) // 关闭弹框
              this.$parent.getDeptTree() // 调用父组件的刷新方法
            })
          } else { // 有id就是编辑
            update(this.formData).then(res => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$layer.close(this.layerid) // 关闭弹框
              this.$parent.getDeptTree() // 调用父组件的刷新方法
            })
          }
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>

</style>
